home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 172_01 / eclosu.c < prev    next >
Text File  |  1980-01-01  |  3KB  |  81 lines

  1. /*
  2.   HEADER:              CUG  nnn.nn;
  3.   TITLE:               LEX - A Lexical Analyser Generator
  4.   VERSION:             1.1 for IBM-PC
  5.   DATE:                Jan 30, 1985
  6.   DESCRIPTION:         A Lexical Analyser Generator. From UNIX
  7.   KEYWORDS:            Lexical Analyser Generator YACC C PREP
  8.   SYSTEM:              IBM-PC and Compatiables
  9.   FILENAME:            ECLOSU.C
  10.   WARNINGS:            This program is not for the casual user. It will
  11.                        be useful primarily to expert developers.
  12.   CRC:                 N/A
  13.   SEE-ALSO:            YACC and PREP
  14.   AUTHORS:             Charles H. Forsyth
  15.                        Scott Guthery 11100 leafwood lane Austin, TX 78750
  16.                        Andrew M. Ward, Jr.  Houston, Texas (Modifications)
  17.   COMPILERS:           LATTICE C
  18.   REFERENCES:          UNIX Systems Manuals -- Lex Manual on distribution disks
  19. */
  20. /*
  21.  * Copyright (c) 1978 Charles H. Forsyth
  22.  *
  23.  * Andrew M. Ward 22 Jun 86 -- Added type cast and argumnet types
  24.  *                   to newset declaration.     
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <assert.h>
  30. #include "lexlex.h"
  31.  
  32.  
  33. /*
  34.  * Construct the epsilon closure of a given set; this is the set of states
  35.  * that may be reached by some number of epsilon transitions from that state.
  36.  */
  37.  
  38. extern struct set *newset(struct nfa **, int, int);
  39.  
  40. struct set *eclosure(t)
  41. struct set *t;
  42. {
  43.         struct nfa *np, *xp;
  44.         int i;
  45.         struct set *tt; /* Return value */
  46.         struct nfa **sp, **tp, **ip, *stack[MAXNFA], *temp[MAXNFA];
  47.  
  48. #ifdef DEBUG
  49.        /* Test is points to valid region of memory */
  50.        assert( isdata( (char *)t, sizeof( struct set ) ) );
  51. #endif
  52.         tp = &temp[0];
  53.  
  54.         for(sp = &stack[0], i = 0; i < t->s_len; i++)
  55.         {
  56.              if( sp <= &stack[ MAXNFA ] )  *tp++ = *sp++ = t->s_els[i];
  57.              else {
  58.                     f_error("Stack overflow in `eclosure'","");
  59.              }
  60.         }
  61.         while(sp > stack) {
  62.                 np = *(--sp);
  63.                 if (np->n_char == EPSILON)
  64.                 for(i = 0; i < 2; i++)
  65.                         if(xp = np->n_succ[i]) {
  66.                                 for(ip = &temp[0]; ip < tp;)
  67.                                         if(*ip++ == xp)
  68.                                                 goto cont;
  69.                                 if(tp>= &temp[MAXNFA]) {
  70.                                         f_error("eclosure: list overflow","");
  71.                                 }
  72.                                 *sp++ = *tp++ = xp;
  73.                         cont:;
  74.                         }
  75.         }
  76.  
  77.         /* 'tt' is a pointer to allocated storage */
  78.         tt = newset(temp, (int)(tp - temp), 1);
  79.         return((struct set *)tt);
  80. }
  81.